<?php
class indexAction extends myAction {
	
	public function execute($request) {
		parent::execute($request);		
		$page = $request->getParameter('page');
		$ed_keywords = $request->getParameter('ed_keywords');
		$ed_type = $request->getParameter('ed_type');
		$ed_remarks = $request->getParameter('ed_remarks');		
		$ed_price_range = $request->getParameter('ed_price_range');
		$ed_series = $request->getParameter('ed_series');
						
		if ($page <= 0)
			$page = 1; 
							
		$query = Doctrine::getTable('EdmicItems')
			->createQuery('a')
			->where('1')			
			;
		if ($ed_keywords != '') {
			$query->andWhere('(name like \'%'.$ed_keywords.'%\'')
				->orWhere('type like \'%'.$ed_keywords.'%\'')
				->orWhere('polar like \'%'.$ed_keywords.'%\'')
				->orWhere('series like \'%'.$ed_keywords.'%\'')
				->orWhere('content like \'%'.$ed_keywords.'%\'')
				->orWhere('content_en like \'%'.$ed_keywords.'%\'')
				->orWhere('remarks like \'%'.$ed_keywords.'%\')')
			;
			$this->ed_keywords = $ed_keywords;
		}
		if ($ed_series != '') {
			$query->andWhere('series = \''.$ed_series.'\'');
			$this->ed_series = $ed_series;
		}
		if ($ed_type != '') {
			switch ($ed_type) {
				case '1': // 电子管
					$query->andWhere('type = \'电子管\'');
					break;
				case '2': // 晶体管
					$query->andWhere('type = \'晶体管\'');
					break;
			}
			$this->ed_type = $ed_type;
		}
		if ($ed_price_range != '') {
			switch ($ed_price_range) {
				case '-1500': // 1500元以下
					$query->andWhere('price <= 1500');
					break;
				case '1500-4000': //
					$query->andWhere('price >= 1500 and price <= 4000');
					break;
				case '4000-': //
					$query->andWhere('price >= 4000');
					break;
			}
			$this->ed_price_range = $ed_price_range;
		}
		if ($ed_remarks != '') {
			//$query->andWhere('remarks like \'%'.$ed_remarks.'%\'');
			$this->ed_remarks = $ed_remarks;
		}
		$perpage = 6;
		$total_count = $query->count();	
		if ($total_count % $perpage == 0)
			$total_page = $total_count/$perpage;
		else
			$total_page = ceil($total_count/$perpage);	
		//var_dump($query->getDql());
		if ($page > $total_page)
			$page = $total_page;		
		
		
		$pager = new Doctrine_Pager($query,$page,$perpage);
		$items = $pager->execute();		
		
		$begin_index = ($page-1)*$perpage + 1;		
		$end_index = ($page*$perpage);		
		if ($end_index > $total_count)
			$end_index = $total_count;
		if ($begin_index > $end_index)
			$begin_index = $end_index;
				
		$pager_params = array(
			'page' => $page,
			'perpage' => $perpage,
			'items_count' => $total_count,
			'begin_index' => $begin_index,
			'end_index' => $end_index,
			'total_page' => $total_page
		);

		//var_dump($pager_params);
		$this->pager_params = $pager_params;
		$this->page = $page;
		$this->items = $items;
		$this->lang = $this->sess_lang;		
	}
	
}